package com.zzyy.recharge.service.impl;

import com.zzyy.recharge.dao.AccountRechargeDao;
import com.zzyy.recharge.entity.AccountRecharge;
import com.zzyy.recharge.service.AccountRechargeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @auther zzyy
 * @create 2023-03-26 12:18
 */
@Service
@Slf4j
public class AccountRechargeServiceImpl implements AccountRechargeService
{
    @Resource AccountRechargeDao accountRechargeDao;
    @Resource RocketMQTemplate rocketMQTemplate;

    @Override
    public AccountRecharge insertAccountRecharge(AccountRecharge accountRecharge)
    {
        int success = accountRechargeDao.insertAccountRecharge(accountRecharge.getId(), accountRecharge.getAccountNo(), accountRecharge.getRechargeAmount(), "success");
        if(success>0){
            //发送通知,使用普通消息发送通知
            accountRecharge.setResult("success");
            rocketMQTemplate.convertAndSend("topic_notifymsg",accountRecharge);
            return accountRecharge;
        }
        return null;
    }

    @Override
    public AccountRecharge getAccountRecharge(String txNo)
    {
        if (txNo.equals("44.44")) {
            throw new RuntimeException("-----移动侧故意出错测试，error,o(╥﹏╥)o");
        }
        if (txNo.equals("99.99")) {
            //暂停几秒钟线程
            try { TimeUnit.SECONDS.sleep(9); } catch (InterruptedException e) { e.printStackTrace(); }
        }
        return accountRechargeDao.findByIdTxNo(txNo);
    }
}
